/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package softwareControl;

import grammaticalClasses.event.Events;
import grammaticalClasses.nwConsole.Machine;
import grammaticalClasses.nwConsole.ParseException;
import grammaticalClasses.nwConsole.TokenMgrError;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author Heitor
 */
public final class SearchCommands {
    
    private String directory = "";
    private String path = "";
    private String text;
    
    private ArrayList<String> commandsRequest;
    private ArrayList<Object> commandsAnswer;
    
    private JFileChooser fileChooser;
    
    private Machine machine;
    private Events events;
    
    public SearchCommands(){
        
        putDirectory();
        configPath();
    }
    
    public SearchCommands(String directory){
        
        this.directory = directory;
        configPath();
    }
    
    public SearchCommands(ArrayList<String> commandsRequest){
        
        this.commandsRequest = commandsRequest;
        putDirectory();
        configPath();
    }
    
    public SearchCommands(String directory, ArrayList<String> commandsRequest){
    
        this.directory = directory;
        this.commandsRequest = commandsRequest;

        configPath();
    }
    
    private void configPath() {
        
        commandsAnswer = new ArrayList<>();
        for(int i=0; i< commandsRequest.size(); i++){
            commandsAnswer.add(new Object());
        }
        
//        configPathNwconsoleOut();
//        execCommandsNwconsoleOut();
        configPathEvents();
        execCommandsEvents();
        configPathDdc();
        execCommandsDCC();
        
//        //Codigo Provisorio
//        String[] aux2 = System.getProperty("user.dir").split("\\\\");
//        String pathAux = "";
    }
    
    private void configPathNwconsoleOut(){
        try {
            path = directory + "/tmp/snapshot/nwconsoleOut.txt";
            InputStream input = new FileInputStream(path);
            openPathNwconsoleOut(input);
        } catch (IOException e1){
            try{
                path = directory + "/pm_root/tmp/snapshot/nwconsoleOut.txt";
                InputStream input = new FileInputStream(path);
                openPathNwconsoleOut(input);
            } catch (IOException e2){
                try{
                    path = directory + "/pm_root/tmp/snapshot/snapshot/nwconsoleOut.txt";
                    InputStream input = new FileInputStream(path);
                    openPathNwconsoleOut(input);
                } catch (IOException e3){
                    System.out.println("Não encontrou Diretório. " + e3.toString());
                    putDirectory(); 
                }
            }
        }
    }
    
    private void configPathEvents(){
        boolean testEvents = true;
        String nameFile = "event";
        int indice = 0;
        
        text = "";
        
        while(testEvents){
            try {
                path = directory + "/tmp/snapshot/" + nameFile + ".log";
                InputStream input = new FileInputStream(path);
                openPathEvents(input);
            } catch (IOException e1){
                try{
                    path = directory + "/pm_root/tmp/snapshot/" + nameFile + ".log";
                    InputStream input = new FileInputStream(path);
                    openPathEvents(input);
                } catch (IOException e2){
                    try{
                        path = directory + "/pm_root/tmp/snapshot/snapshot/" + nameFile + ".log";
                        InputStream input = new FileInputStream(path);
                        openPathEvents(input);
                    } catch (IOException e3){
                        testEvents = false;
                        //putDirectory(); 
                    }
                }
            }
            
            indice++;
            nameFile = nameFile.concat("" + indice);
        }
    }
    
    private void configPathDdc(){
        
    }
    
    private void putDirectory(){
    
        fileChooser = new javax.swing.JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal2 = fileChooser.showOpenDialog(fileChooser);
        directory = "";
        if (returnVal2 == JFileChooser.APPROVE_OPTION) {
            File fileLoad = fileChooser.getSelectedFile();
            directory = fileLoad.getAbsolutePath();
        }
    }
    
    /**
     * Abre o arquivo, de acordo com o diretório, e gera o texto a ser processado.
     * 
     * @throws FileNotFoundException
     * @throws IOException
     * @throws IOException 
     */
    private void openPathNwconsoleOut(InputStream input) throws FileNotFoundException, IOException{
        
        StringBuilder sb;

        sb = new StringBuilder();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while ((line = reader.readLine()) != null) {
            switch (line) {
                case "> ":
                    sb.append("/&/").append(line).append("/a2/").append("\n");
                    break;
                case "^C":
                    sb.append("/a2/").append(line).append("/&/").append("\n");
                    break;
                default:
                    sb.append(line).append("\n");
                    break;
            }

        }

        text = sb.toString();
    }
    
    private void openPathEvents(InputStream input) throws FileNotFoundException, IOException{
    
        StringBuilder sb = new StringBuilder();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        input.close();
    
        if(text != null){
            text = text + sb.toString();
        } else{
            text = sb.toString();
        }
        
    }
    
    private void execCommandsNwconsoleOut(){
    try {
        execParsersNwconsoleOut();
        if (commandsRequest != null){
            for(int i=0; i<commandsRequest.size(); i++){
            switch (commandsRequest.get(i)) {
                case "ShowCliSessions":
                    machine.showCliSessions();
                    getCommandsAnswer().set(i, machine.getsCliSessionsList());
                    break;
                case "ShowFrame":
                    machine.showFrame();
                    getCommandsAnswer().set(i, machine.getsFrameList());
                    break;
                case "ShowGw":
                    machine.showGw();
                    getCommandsAnswer().set(i, machine.getsGwList());
                    break;
                case "ShowMote":
                    machine.showMote();
                    getCommandsAnswer().set(i, machine.getsMoteList());
                    break;
                case "ShowMsg":
                    machine.showMsg();
                    getCommandsAnswer().set(i, machine.getsMsgList());
                    break;
                case "ShowMsgV":
                    machine.showMsgV();
                    getCommandsAnswer().set(i, machine.getsMsgVList());
                    break;
                case "ShowOptim":
                    machine.showOptim();
                    getCommandsAnswer().set(i, machine.getsOptimList());
                    break;
                case "ShowSessions":
                    machine.showSessions();
                    getCommandsAnswer().set(i, machine.getsSessionsList());
                    break;
                case "ShowStatCur":
                    machine.showStatCur();
                    getCommandsAnswer().set(i, machine.getsStatCurList());
                    break;
                case "ShowStatNet":
                    machine.showStatNet();
                    getCommandsAnswer().set(i, machine.getsStatNetList());
                    break;
                case "ShowStatDay":
                    machine.showStatDay();
                    getCommandsAnswer().set(i, machine.getsStatDayList());
                    break;
                case "ShowStatLife":
                    machine.showStatLife();
                    getCommandsAnswer().set(i, machine.getsStatLifeList());
                    break;
                case "ShowStatShort":
                    machine.showStatShort();
                    getCommandsAnswer().set(i, machine.getsStatShortList());
                    break;
                case "ShowStatus":
                    machine.showStatus();
                    getCommandsAnswer().set(i, machine.getsStatusList());
                    break;
                case "ShowTime":
                    machine.showTime();
                    getCommandsAnswer().set(i, machine.getsTimeList());
                    break;
                case "ShowVer":
                    machine.showVer();
                    getCommandsAnswer().set(i, machine.getsVerList());
                    break;
                case "GetAcls":
                    machine.getAcls();
                    getCommandsAnswer().set(i, machine.getgAclsList());
                    break;
                case "GetAlarms":
                    machine.getAlarms();
                    getCommandsAnswer().set(i, machine.getgAlarmsList());
                    break;
                case "GetBlacklist":
                    machine.getBlacklist();
                    getCommandsAnswer().set(i, machine.getgBlacklistList());
                    break;
                case "GetNetwork":
                    machine.getNetwork();
                    getCommandsAnswer().set(i, machine.getgNetworkList());
                    break;
                case "GetPaths":
                    machine.getPaths();
                    getCommandsAnswer().set(i, machine.getgPathsList());
                    break;
                case "GetRedundancy":
                    machine.getRedundancy();
                    getCommandsAnswer().set(i, machine.getgRedundancyList());
                    break;
                case "GetSecurity":
                    machine.getSecurity();
                    getCommandsAnswer().set(i, machine.getgSecurityList());
                    break;
                case "GetSla":
                    machine.getSla();
                    getCommandsAnswer().set(i, machine.getgSlaList());
                    break;
                case "GetSystem":
                    machine.getSystem();
                    getCommandsAnswer().set(i, machine.getgSystemList());
                    break;
                case "GetUsers":
                    machine.getUsers();
                    getCommandsAnswer().set(i, machine.getgUsersList());
                    break;
                default:
                    break;
            }
            }
        }
    } catch (ParseException ex) {
        System.out.println("");
    } catch (TokenMgrError ex) {
        System.out.println("");
    } catch (FileNotFoundException ex) {
        System.out.println("");
    } catch (IOException ex) {
        System.out.println("");
    }
    
    }
    
    private void execCommandsEvents(){
    
        try{
        execParsersEvents();
        if (commandsRequest != null){
            for(int i=0; i<commandsRequest.size(); i++){
            switch (commandsRequest.get(i)) {
                case "AlarmOpen":
                    getCommandsAnswer().set(i, events.getAlarmOpen());
                    break;
                case "AlarmClose":
                    getCommandsAnswer().set(i, events.getAlarmClose());
                    break;
                default:
                    break;
            }
            }
        }
        } catch (Exception e){
            System.out.println("[Error-execCommandsEvents]"+ e.toString());
        }
        
    }
    
    private void execCommandsDCC(){}
    
    private void execParsersNwconsoleOut(){
        
        try {
            machine = new Machine(text);
        } catch (ParseException ex) {
            System.out.println("[Error execParsersNwconsoleOut]" + ex.toString());
        } catch (TokenMgrError ex) {
            System.out.println("[Error execParsersNwconsoleOut]" + ex.toString());
        } catch (FileNotFoundException ex) {
            System.out.println("[Error execParsersNwconsoleOut]" + ex.toString());
        } catch (IOException ex) {
            System.out.println("[Error execParsersNwconsoleOut]" + ex.toString());
        }
    }
    
    private void execParsersEvents(){
        try {
            events = new Events(text);
        } catch (UnsupportedEncodingException ex) {
            System.out.println("[Error execParsersEvents]" + ex.toString());
        } catch (grammaticalClasses.event.ParseException ex) {
            System.out.println("[Error execParsersEvents]" + ex.toString());
        } catch (TokenMgrError ex) {
            System.out.println("[Error execParsersEvents]" + ex.toString());
        }
    }
    
    /**
     * @return the commandsAnswer
     */
    public ArrayList<Object> getCommandsAnswer() {
        return commandsAnswer;
    }

    /**
     * @param commandsRequest the commandsRequest to set
     */
    public void setCommandsRequest(ArrayList<String> commandsRequest) {
        
        this.commandsRequest = commandsRequest;
        configPath();
    }
    
}
